﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
using MapEntities;
using System.Collections.Generic;
using System.Windows.Browser;
using System.Windows.Media.Effects;

namespace VirtualEarth.Business
{
    public abstract class GeoItem
    {
        protected Map map;

        public LayerItem Item { get; private set; }
        public GeoLayer Layer { get; internal set; }

        protected UIElement BaseElement
        {
            get
            {
                if (Layer == null)
                    return map;
                else return Layer;
            }
        }

        protected  void AddToBaseControl(UIElement element)
        {
            if (Layer == null)
                map.Children.Add(element);
            else 
            Layer.Children.Add(element);

        }


        protected Border GetTextBlock(string text, double Longitude, double Latitude)
        {      
            TextBlock xtb = new TextBlock();
            xtb.Text = text;            
            xtb.TextAlignment = TextAlignment.Center;
            xtb.TextWrapping = TextWrapping.Wrap;
            xtb.Foreground = new SolidColorBrush(Colors.White);            
            DropShadowEffect xdse = new DropShadowEffect();
            xdse.Color = Colors.Black;
            xdse.BlurRadius = 200;
            xdse.ShadowDepth = 2;
            xtb.Effect = xdse;
            xtb.FontSize = 12;
            xtb.FontWeight = FontWeights.Normal;
            xtb.FontFamily = new FontFamily("Tahoma");
            xtb.FontStyle = FontStyles.Normal;            
            Border xb = new Border();
            xb.Background = new SolidColorBrush(Colors.Black);
            xb.Opacity = 0.6;
            xb.BorderBrush = new SolidColorBrush(Colors.Red);
            xb.BorderThickness = new Thickness(1);
            xb.CornerRadius = new CornerRadius(4);
            xb.MouseEnter += new MouseEventHandler(xb_MouseEnter);
            xb.MouseLeave += new MouseEventHandler(xb_MouseLeave);
            xb.Child = xtb;
            xb.SetValue(MapLayer.PositionProperty, new Microsoft.Maps.MapControl.Location(Latitude, Longitude));
            xb.SetValue(MapLayer.PositionOriginProperty, PositionOrigin.Center);
            Canvas.SetZIndex(xb, 2);
            return xb;
        }

        void xb_MouseLeave(object sender, MouseEventArgs e)
        {
            Border xtb = (Border)sender;
            Canvas.SetZIndex(xtb, Canvas.GetZIndex(xtb) - 1);            
            xtb.RenderTransform = null;
            
        }

        void xb_MouseEnter(object sender, MouseEventArgs e)
        {
            Border xtb = (Border)sender;
            Canvas.SetZIndex(xtb, Canvas.GetZIndex(xtb) + 1);
            ScaleTransform xst = new ScaleTransform();
            xst.ScaleX = 2.0;
            xst.ScaleY = 2.0;
            xst.CenterX = xtb.Height / 2;
            xst.CenterY = xtb.Height / 2;
            xtb.RenderTransform = xst;
            
        }

  

        protected GeoItem(Map map, LayerItem item)
        {
            this.map = map;
            this.Item = item;            
        }
        public abstract void Visualize();

        protected virtual void DoBehaviour(ClickBehaviour behaviour)
        {
            if (!string.IsNullOrEmpty(behaviour.RedirectAddress))
            {
                if (behaviour.RedirectAddress.StartsWith("javascript"))
                    HtmlPage.Window.Eval(behaviour.RedirectAddress);
                else HtmlPage.Window.Navigate(new Uri(behaviour.RedirectAddress), "_blank");
            };
            if (behaviour.ZoomToLevel.HasValue && behaviour.ZoomToLevel.Value != 0)
            {
                map.ZoomLevel = behaviour.ZoomToLevel.Value;
            }
        }

        protected virtual void DoMouseLeave(LayerItemBehaviour behavior)
        {

        }

        protected virtual void DoMouseEnter(LayerItemBehaviour behavior)
        {

        }

        public double GetOpacity(TransparencyLevel level)
        {
            return 1.0 - Convert.ToDouble(((int)level)) * 0.25;
        }

        public Color HexToColor(string hexValue)
        {
            try
            {
                hexValue = hexValue.Replace("#", "");
                byte position = 0;
                byte alpha = System.Convert.ToByte("ff", 16);

                if (hexValue.Length == 8)
                {
                    // get the alpha channel value
                    alpha = System.Convert.ToByte(hexValue.Substring(position, 2), 16);
                    position = 2;
                }

                // get the red value
                byte red = System.Convert.ToByte(hexValue.Substring(position, 2), 16);
                position += 2;

                // get the green value
                byte green = System.Convert.ToByte(hexValue.Substring(position, 2), 16);
                position += 2;

                // get the blue value
                byte blue = System.Convert.ToByte(hexValue.Substring(position, 2), 16);

                // create the Color object
                Color color = Color.FromArgb(alpha, red, green, blue);

                // create the SolidColorBrush object
                return color;
            }
            catch
            {
                return Color.FromArgb(255, 251, 237, 187);
            }
        }
    }
}
